package com.pig4cloud.pig.module.controller.devices;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.DateUtils;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.DeviceDrawRecord;
import com.pig4cloud.pig.module.service.devices.IDeviceDrawRecordService;
import com.vdurmont.emoji.EmojiParser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户领取口罩和袋子的记录Controller
 *
 * @author ruoyi
 * @date 2022-06-13
 */
@Validated
@Api(value = "用户领取口罩和袋子的记录控制器", tags = {"用户领取口罩和袋子的记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/device/draw/record")
public class DeviceDrawRecordController {

    private final IDeviceDrawRecordService deviceDrawRecordService;

    /**
     * 查询用户领取口罩和袋子的记录列表
     */
    @ApiOperation("查询用户领取口罩和袋子的记录列表")
    @PreAuthorize("@pms.hasPermission('module:drawRecord:list')")
    @GetMapping("/list")
    public TableDataInfo<DeviceDrawRecord> list(@Validated DeviceDrawRecord deviceDrawRecord) {
        QueryWrapper<DeviceDrawRecord> qw = new QueryWrapper<>();
        Map<String,Object> map = new HashMap<>();
        if(deviceDrawRecord.getGetType() !=null){
        	qw.eq("get_type",deviceDrawRecord.getGetType());
			map.put("get_type",deviceDrawRecord.getGetType());
		}

		if(deviceDrawRecord.getDeviceId() !=null){
			qw.eq("device_id",deviceDrawRecord.getDeviceId());
			map.put("device_id",deviceDrawRecord.getDeviceId());
		}
		if(StrUtil.isNotEmpty(deviceDrawRecord.getBeginDate())){
			qw.between("create_time",deviceDrawRecord.getBeginDate() + " 00:00:00",deviceDrawRecord.getEndDate() + " 23:59:59");
			map.put("begin_time", deviceDrawRecord.getBeginDate() + " 00:00:00");
			map.put("end_time", deviceDrawRecord.getEndDate() + " 23:59:59");
		}

        TableDataInfo<DeviceDrawRecord> td = PageUtils.buildDataInfo(deviceDrawRecordService.selectRecordList(PageUtils.buildPage(),map));
        List<DeviceDrawRecord> list = td.getData();
        if(CollUtil.isNotEmpty(list)){
        	for (DeviceDrawRecord drawRecord : list){
				if(StrUtil.isNotEmpty(drawRecord.getNickName())){
					drawRecord.setNickName(EmojiParser.parseToUnicode(drawRecord.getNickName()));
				}
			}
		}
        return td;
    }

    /**
     * 导出用户领取口罩和袋子的记录列表
     */
    @ApiOperation("导出用户领取口罩和袋子的记录列表")
    @PreAuthorize("@pms.hasPermission('module:drawRecord:export')")
    @SysLog("用户领取口罩和袋子的记录")
    @GetMapping("/export")
    public List export(@Validated DeviceDrawRecord deviceDrawRecord) {
		QueryWrapper<DeviceDrawRecord> qw = new QueryWrapper<>();
		Map<String,Object> map = new HashMap<>();
		if(deviceDrawRecord.getGetType() !=null){
			qw.eq("get_type",deviceDrawRecord.getGetType());
			map.put("get_type",deviceDrawRecord.getGetType());
		}

		if(deviceDrawRecord.getDeviceId() !=null){
			qw.eq("device_id",deviceDrawRecord.getDeviceId());
			map.put("device_id",deviceDrawRecord.getDeviceId());
		}

		if(StrUtil.isNotEmpty(deviceDrawRecord.getBeginDate())){
			qw.between("create_time",deviceDrawRecord.getBeginDate() + " 00:00:00",deviceDrawRecord.getEndDate() + " 23:59:59");
			map.put("begin_time", deviceDrawRecord.getBeginDate() + " 00:00:00");
			map.put("end_time", deviceDrawRecord.getEndDate() + " 23:59:59");
		}
        List<DeviceDrawRecord> list = deviceDrawRecordService.selectRecordList(map);
		if(CollUtil.isNotEmpty(list)){
			for (DeviceDrawRecord drawRecord : list){
				drawRecord.setCreateTimeStr(DateUtils.getTime(drawRecord.getCreateTime()));

				if(StrUtil.isNotEmpty(drawRecord.getNickName())){
					drawRecord.setNickName(EmojiParser.parseToUnicode(drawRecord.getNickName()));
				}

				if(drawRecord.getGetType() == 0){
					drawRecord.setGetTypeName("环保袋");
				} else if(drawRecord.getGetType() == 1){
					drawRecord.setGetTypeName("口罩");
				}
			}
		}
        // ExcelUtil<DeviceDrawRecord> util = new // ExcelUtil<DeviceDrawRecord>(DeviceDrawRecord.class);
        return  list;// util.exportExcel(list, "用户领取口罩和袋子的记录");
    }

    /**
     * 获取用户领取口罩和袋子的记录详细信息
     */
    @ApiOperation("获取用户领取口罩和袋子的记录详细信息")
    @PreAuthorize("@pms.hasPermission('module:drawRecord:query')")
    @GetMapping("/{id}")
    public R<DeviceDrawRecord> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") String id) {
        DeviceDrawRecord deviceDrawRecord = deviceDrawRecordService.getById(id);
        return R.ok(deviceDrawRecord);
    }

    /**
     * 新增用户领取口罩和袋子的记录
     */
    @ApiOperation("新增用户领取口罩和袋子的记录")
    @PreAuthorize("@pms.hasPermission('module:drawRecord:add')")
    @SysLog("用户领取口罩和袋子的记录")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody DeviceDrawRecord deviceDrawRecord) {
        return R.okByRow(deviceDrawRecordService.save(deviceDrawRecord) ? 1 : 0);
    }

    /**
     * 修改用户领取口罩和袋子的记录
     */
    @ApiOperation("修改用户领取口罩和袋子的记录")
    @PreAuthorize("@pms.hasPermission('module:drawRecord:edit')")
    @SysLog("用户领取口罩和袋子的记录")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody DeviceDrawRecord deviceDrawRecord) {
        return R.okByRow(deviceDrawRecordService.updateById(deviceDrawRecord) ? 1 : 0);
    }

    /**
     * 删除用户领取口罩和袋子的记录
     */
    @ApiOperation("删除用户领取口罩和袋子的记录")
    @PreAuthorize("@pms.hasPermission('module:drawRecord:remove')")
    @SysLog("用户领取口罩和袋子的记录")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable String[] ids) {
        return R.okByRow(deviceDrawRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
